<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SalesForceEventView
 *
 * @author HP
 */
class SalesForceEventView {
    private $insightUser;
    private static $arrFieldMap = array (
        "Id" => "Id",
        "Subject" => "Subject",
        "Location" => "Location",
        "IsAllDayEvent" => "IsAllDayEvent",
        "ActivityDateTime" => "ActivityDateTime",
        "ActivityDate" => "ActivityDate",
        "StartDateTime" => "StartDateTime",
        "EndDateTime" => "EndDateTime",
        "Description" => "Description",
        "OwnerId" => "OwnerId",
        "Type" => "Type",
        "IsPrivate" => "IsPrivate",
        "ShowAs" => "ShowAs",
        "IsDeleted" => "IsDeleted",
        "LastModifiedDate" => "LastModifiedDate",
        "IsRecurrence" => "IsRecurrence",
        "ReminderDateTime" => "ReminderDateTime",
        "IsReminderSet" => "IsReminderSet",
        "WhoId" => "WhoId"
    );
    
    private static $arrSfToGoogleMapping = array (
        //"Id" => "Id",
        "ActivityDate" => "start_Date",
        "ActivityDateTime" => "start_DateTime",
        "Description" => "description",
        "EndDateTime" => "end_DateTime",
        "IsAllDayEvent" => "IsAllDayEvent",
        "IsRecurrence" => "IsRecurrence",
        "IsReminderSet" => "IsReminderSet",
        "Location" => "location",
        "ReminderDateTime" => "reminderDateTime",
        "ShowAs" => "showAs",
        "StartDateTime" => "start_DateTime",
        "Subject" => "Type",
        "WhoId" => "WhoId"
    );
    
    public function __construct(InsightUser $insightUser) {
        $this->insightUser = $insightUser;
    }
    
    public function createModel($record) {
        $eventModel = new SalesForceEventPoco();
        $eventModel->setId($record->Id);
        $eventModel->setSubject($record->fields->Subject);
        $eventModel->setLocation($record->fields->Location);
        $eventModel->setIsAllDayEvent($record->fields->IsAllDayEvent);
        $eventModel->setActivityDateTime($record->fields->ActivityDateTime);
        $eventModel->setActivityDate($record->fields->ActivityDate);
        $eventModel->setStartDateTime($record->fields->StartDateTime);
        $eventModel->setEndDateTime($record->fields->EndDateTime);
        $eventModel->setDescription($record->fields->Description);
        $eventModel->setOwnerId($record->fields->OwnerId);
        $eventModel->setType("Meeting");
        $eventModel->setIsPrivate($record->fields->IsPrivate);
        $eventModel->setShowAs($record->fields->ShowAs);
        $eventModel->setIsDeleted($record->fields->IsDeleted);
        $eventModel->setLastModifiedDate($record->fields->LastModifiedDate);
        $eventModel->setIsRecurrence($record->fields->IsRecurrence);
        $eventModel->setReminderDateTime($record->fields->ReminderDateTime);
        $eventModel->setIsReminderSet($record->fields->IsReminderSet);
        $eventModel->setWhoId($record->fields->WhoId);
        
        $eventModel->Attendees = array();
        if(isset($records->fields->EventAttendees)){
            
            foreach($records->fields->EventAttendees as $attendee) {
                $attendeeModel = new SalesforceEventAttendeePoco();
                $attendeeModel->Id = $attendee->fields->Id;
                $attendeeModel->EventId = $attendee->fields->EventId;
                $attendeeModel->AttendeeId = $attendee->fields->AttendeeId;
                $attendeeModel->Name = $attendee->fields->Attendee->fields->Name;
                $attendeeModel->Status = $attendee->fields->Status;
                $attendeeModel->RespondedDate = $attendee->fields->RespondedDate;
                $attendeeModel->Response = $attendee->fields->Response;
                $attendeeModel->LastModifiedDate = $attendee->fields->LastModifiedDate;
                $attendeeModel->IsDeleted = $attendee->fields->IsDeleted;
                
                $eventModel->Attendees[] = $attendeeModel;
            }
        }
        
        return $eventModel;
    }
    
    public function createModelFromGoogleEvent(GoogleEvent $googleEvent) {
        $eventModel = new SalesforceEventPoco();
        //$eventModel->setId($googleEvent->event_Id);
        $eventModel->setSubject($googleEvent->summary);
        $eventModel->setLocation($googleEvent->location);
        $eventModel->setIsAllDayEvent($googleEvent->IsAllDayEvent);
        $eventModel->setActivityDateTime($googleEvent->start_DateTime);
        $eventModel->setActivityDate($googleEvent->start_Date);
        $eventModel->setStartDateTime($googleEvent->start_DateTime);
        $eventModel->setEndDateTime($googleEvent->end_DateTime);
        $eventModel->setDescription($googleEvent->description);
        $eventModel->setOwnerId($this->insightUser->user_id);
        $eventModel->setType($googleEvent->Type);
        //$eventModel->setIsPrivate(); //not part of specs
        $eventModel->setShowAs($googleEvent->showAs);
        $eventModel->setIsDeleted($googleEvent->eventstatus=="cancelled"?true:false);
        $eventModel->setLastModifiedDate($googleEvent->updated);
        $eventModel->setIsRecurrence($googleEvent->IsRecurrence);
        $eventModel->setReminderDateTime($googleEvent->reminderDateTime);
        $eventModel->setIsReminderSet($googleEvent->IsReminderSet);
        $eventModel->setGoogleEventUrl($googleEvent->event_url);
        
        if(isset($googleEvent->attendees)){
            $Attendees = array();
            foreach($googleEvent->attendees as $attendee) {
                $attendeeModel = new SalesforceEventAttendeePoco();
                $attendeeModel->Name = $attendee->displayName;
                $attendeeModel->Status = $attendee->responseStatus;
                $Attendees[] = $attendeeModel;
            }
            if(count($Attendees) > 0) {
                $eventModel->setAttendees($Attendees);
            }
        }
        return $eventModel;
    }
    
    
    public function createStdObject(SalesForceEventPoco $event) {
        $record = new stdClass();
        $record->type = 'Event';
        
        $recordId = $event->getId();
        if(Util::isNullOrEmpty($recordId)){
            $record->Id = $recordId;
        }
        $record->fields["Subject"] = $event->getSubject();
        $record->fields["Location"] = $event->getLocation();
        $record->fields["IsAllDayEvent"] = CalendarSyncUtil::booleanToStr($event->getIsAllDayEvent());
        $record->fields["ActivityDateTime"] = CalendarSyncUtil::setEmptyToNull($event->getActivityDateTime());
        $record->fields["ActivityDate"] = CalendarSyncUtil::setEmptyToNull($event->getActivityDate());
        $record->fields["StartDateTime"] = CalendarSyncUtil::setEmptyToNull($event->getStartDateTime());
        $record->fields["EndDateTime"] = CalendarSyncUtil::setEmptyToNull($event->getEndDateTime());
        //$record->fields["Description"] = $event->getDescription();
        $record->fields["OwnerId"] = $event->getOwnerId();
        $record->fields["Type"] = $event->getType();
        $record->fields["IsPrivate"] = CalendarSyncUtil::booleanToStr($event->getIsPrivate());
        $record->fields["ShowAs"] = $event->getShowAs();
        //$record->fields["IsDeleted"] = CalendarSyncUtil::booleanToStr($event->getIsDeleted());
        //$record->fields["LastModifiedDate"] = CalendarSyncUtil::setEmptyToNull($event->getLastModifiedDate());
        $record->fields["IsRecurrence"] = CalendarSyncUtil::booleanToStr($event->getIsRecurrence());
        $record->fields["ReminderDateTime"] = CalendarSyncUtil::setEmptyToNull($event->getReminderDateTime());
        $record->fields["IsReminderSet"] = CalendarSyncUtil::booleanToStr($event->getIsReminderSet());
        $record->fields["WhoId"] = $event->getWhoId();
        
        $description = $event->getDescription();
        if(!Util::isNullOrEmpty($description)){
            if(strpos($description, "* End Attendee Responses *")) {
                $description = substr($description, strpos($description, "* End Attendee Responses *")+strlen("* End Attendee Responses *")+1);
            }
        }
        
        $metaData = "View in Google Calendar: ".$event->getGoogleEventUrl()."\r\n";
        if($event->getAttendees()!=null) {
            $metaData .= "* Attendee Responses *\r\n";
            foreach($event->getAttendees() as $attendee) {
                $metaData .= $attendee->Name." : ".$attendee->Status."\r\n";
            }
            $metaData .= "* End Attendee Responses *\r\n";
        }
        
        $record->fields["Description"] = $metaData.$description;
        return $record;
    }
}

?>
